package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Attraction;
import com.example.demo.entity.User;
import com.example.demo.service.AttractionService;
import com.example.demo.service.UserService;
import com.example.demo.utils.JwtUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("")
public class AdminController {
    @Resource
    private UserService userService;
    @Resource
    private AttractionService attractionService;
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session){
        User currentUser = userService.checkExist(user);
        if(currentUser!=null){
            session.setAttribute("user",currentUser);
            return Result.success(currentUser);
        }
        else{
            return Result.error("用户名或密码错误!");
        }
    }

    @GetMapping("/getScenes")
    public Result getAttractions(){
        PageInfo<Attraction> attractionList = attractionService.list();
        if(attractionList != null){
            return Result.success(attractionList);
        }
        else {
            return Result.error("景点列表为空!");
        }
    }

    @GetMapping("/currentUser")
    public Result currentUser(HttpSession session){
        return Result.success(session.getAttribute("user"));
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if(userService.addUser(user)){
            return Result.success();
        }
        return Result.error("添加失败!");
    }
}
